package atom.core3.redis.servlet;

import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import atom.core0.util.StringUtil;
import atom.core3.redis.RedisSession;

public class InitRedisServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(InitRedisServlet.class);
	public void init() throws ServletException {
		super.init();
		
		//初始化redies
		try
		{
			String filename = getInitParameter("filename");
			InputStream inn = getServletContext().getResourceAsStream(filename);
			Properties pps = new Properties();
			pps.load(inn);
			
			String ipports = pps.getProperty("ipports");// getInitParameter("ipports");	
			if(StringUtil.isEmpty(ipports))
			{
				log.info("sentinels是空,InitServlet没有加载Resis服务端");
				return ;
			}
			String masterName = pps.getProperty("masterName");//getInitParameter("masterName");	
			if(StringUtil.isEmpty(masterName))
			{
				log.info("masterName是空,InitServlet没有加载Resis服务端");
				return ;
			}
			String domain = pps.getProperty("domain");//getInitParameter("domain");	
			String password = pps.getProperty("password");//getInitParameter("domain");	
			int ntimeout=0;
			String timeout = pps.getProperty("timeout");//getInitParameter("timeout");	
			if(StringUtil.isEmpty(timeout))
			{
				ntimeout=3600;
			}
			else
			{
				ntimeout = Integer.valueOf(timeout);
			}
			
			String[] ipport = ipports.split(",");
			HashSet<String> sentinels = new HashSet<String>();
			//sentinels.add("192.168.10.202:26379");		
			//sentinels.add("192.168.10.202:26380");	
			for(int i=0;i<ipport.length;i++)
			{
				sentinels.add(ipport[i]);	
			}
			
			RedisSession.initJedisSentinels(masterName, sentinels,password);//"mymaster"
			RedisSession.initSessionTimeout(ntimeout);//3600
			if(!StringUtil.isEmpty(domain))
			{
				RedisSession.initDomain(domain);//".ant.com"
			}			
			
		}
		catch(Throwable e)
		{
			log.error("加载Redis服务端错误",e);
		}

	}

}
